<template>
  <div>
    <div class="sort" @click="changeState">
      <span
        :style="{
          color: selectSort === value && state ? '#6973FF' : '#5D637E',
        }"
        >{{ title }}</span
      >
      <div v-if="state === 'asc'" class="box">
        <a-icon type="caret-up" class="active up" />
        <a-icon type="caret-down" class="negative down" />
      </div>
      <div v-else-if="state === 'desc'" class="box">
        <a-icon type="caret-up" class="negative up" />
        <a-icon type="caret-down" class="active down" />
      </div>
      <div v-else class="box">
        <a-icon type="caret-up" class="normal up" />
        <a-icon type="caret-down" class="normal down" />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Sort',
  components: {},
  props: {
    title: String,
    value: String,
    selectSort: Object,
  },
  data() {
    return {
      state: null,
    }
  },
  computed: {},
  watch: {
    selectSort() {
      if (this.selectSort.value !== this.value) {
        this.state = null
      } else {
        this.state = this.selectSort.state
      }
    },
  },
  mounted() {},
  created() {
    if (this.selectSort.value === this.value) {
      this.state = this.selectSort.state
    }
  },
  methods: {
    changeState() {
      this.state =
        this.state === 'asc' ? 'desc' : this.state === 'desc' ? null : 'asc'
      this.$emit('change-state', { value: this.value, state: this.state })
    },
  },
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped>
.sort {
  align-items: center;
  cursor: pointer;
  display: flex;
  padding: 0 12px;

  .box {
    height: 17px;
    margin-left: 6px;
    position: relative;
    width: 15px;

    .active {
      color: #6773ff;
      font-size: 10px;
      position: absolute;
    }

    .normal {
      font-size: 10px;
      position: absolute;
    }

    .negative {
      color: rgba(92, 99, 128, 0.5);
      font-size: 10px;
      position: absolute;
    }

    .up {
      top: 0;
    }

    .down {
      bottom: 0;
    }
  }
}
</style>
